import { defineStore } from 'pinia'
import { getAllBreadcrumbList, getFlatMenuList, getShowMenuList, transformObject } from '@/utils'
import { mockData } from '../mockData/menu1'
import { apiGetMenu, apiListProject, apiUserAuthRouters} from '@/api/modules/login'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    // 前端项目列表
    projectList: [],
    // 按钮权限列表
    authButtonList: [],
    // 菜单权限列表
    authMenuList: [],
    // 所有权限路由
    allAuthRouters: [],
  }),
  getters: {
    // 前端项目列表
    authProjectListGet: (state) => state.projectList,
    // 按钮权限列表
    authButtonListGet: (state) => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: (state) => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: (state) => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: (state) => getFlatMenuList(state.authMenuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: (state) => getAllBreadcrumbList(state.authMenuList),
    // 所有权限路由
    allRouterListGet: (state) => state.allAuthRouters,
  },
  actions: {
    // 获取前端项目列表
    getListProject() {
      const projectApi = apiListProject({ })
      return new Promise((resolve, reject) => {
        projectApi
          .then((res) => {
            if (res.success) {
              const list = res.data
              console.log('------0001', list, res)
              this.projectList = list
              resolve(list)
            } else {
              console.log('------0002', res)
              resolve([])
            }
          })
          .catch((error: any) => {
            reject(error)
          })
        })
    },
    // // Get AuthMenuList
    getAuthMenuList() {
      const useApi = apiGetMenu({ topNode: 'root' })
      return new Promise((resolve, reject) => {
        useApi
          // .then(() => {
          //   const res: any = mockData
          //   const transformedObject = transformObject(res)
          //   const menuData = transformedObject['children'] || []
          //   this.authMenuList = menuData
          //   resolve(menuData)
          // })
          .then((res) => {
            // const res: any = mockData
            console.log(res,'res123456')
            if(res.success){
              const transformedObject = transformObject(res.data)
              const menuData = transformedObject['children'] || []
              this.authMenuList = menuData
              resolve(menuData)
            }
            
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    },
    getAllAuthRouters() {
      const userAuth = apiUserAuthRouters({ })
      return new Promise((resolve, reject) => {
        userAuth
          // .then(() => {
          //   const res: any = mockData
          //   const transformedObject = transformObject(res)
          //   const menuData = transformedObject['children'] || []
          //   this.authMenuList = menuData
          //   resolve(menuData)
          // })
          .then((res) => {
            // const res: any = mockData
            console.log('menulist--------000', res.data)
            // const transformedObject = transformObject(res.data)
            // const menuData = transformedObject['children'] || []
            // this.authMenuList = menuData
            this.allAuthRouters = res.data
            resolve(res.data)
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    },
       // 获取用户所有页面路由 userAuth/getPageRouters
      //  const getUserAuthRouters = () => {
      //   apiUserAuthRouters({})
      //       .then((res) => {
      //           if (res.data.success) {
      //               usePageData.allRouters = res.data.data
      //           } 
      //       }) 
      //   }
  },
  // persist: piniaPersistConfig('auth'),
})
